﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SPMS.Log.Core;
using SPMS.Log.Utility;

namespace SPMS.Log.Formatter
{
    /// <summary>
    /// 简单日志格式化器
    /// </summary>
    /// <remarks>提供了最简单的日志格式器的实现</remarks>
    /// <history>
    /// [zanezeng]               2009/10/9 12:04    创建
    /// </history>
    public sealed class SimpleFormatter : AbstractFormatter
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="SimpleFormatter"/>的新实例
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/29 14:12    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/9 12:07    创建
        /// </history>
        public SimpleFormatter()
        {
        }

        #endregion

        #region ---Protected Method

        /// <summary>
        /// 将给定的日志条目格式化为一个有序的字符串序列
        /// </summary>
        /// <param name="entry">要进行格式化的日志记录</param>
        /// <returns>描述给定日志记录的一个有序的字符串序列</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:19    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 10:31    创建
        /// </history>
        protected override string FormatEntry( LogEntry entry )
        {
            //使用模板解析日志条目
            return s_Parser.Parse( entry, this.FormatProvider );
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得简单格式化器的默认实例
        /// </summary>
        /// <value>简单格式化器的默认实例</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/13 11:13    创建
        /// </history>
        public static SimpleFormatter Default
        {
            get
            {
                return DEFAULT_INSTANCE;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 默认的实例
        /// </summary>
        private static readonly SimpleFormatter DEFAULT_INSTANCE = new SimpleFormatter();

        /// <summary>
        /// 格式化模板
        /// </summary>
        private const string FORMAT_PATTERN = "{Level}: [{Timestamp:yyyy/MM/dd HH:mm:ss fff}] [{Message}] [{ThreadName}] [{TypeName}:{MethodName}] [{FileName}:{LineNumber}] {NEWLINE}{Exception}";

        /// <summary>
        /// 模板解析器
        /// </summary>
        private static readonly PatternParser s_Parser = new PatternParser( FORMAT_PATTERN );

        #endregion
    }
}
